package at.codebase.web.servlets.user;

import java.util.ArrayList;

import com.google.appengine.api.users.UserServiceFactory;

import at.codebase.web.templates.MainTemplate;
import at.codebase.web.templates.UserProfileTemplate;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.servlet.SecureServlet;

public abstract class AUserProfile extends SecureServlet {

	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ANONYMOUS);
		secLevels.add(UserLevel.REGISTERED);
		secLevels.add(UserLevel.ADMIN);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo, UserLevel level, RequestType requestType) {

		if (requestInfo.isUserLoggedIn()) {
			MainTemplate mainTemplate = new MainTemplate(requestInfo, writer());
			buildPage(mainTemplate, requestInfo, level, requestType);
			mainTemplate.render(requestInfo.getLocale());
		} else {
			// User not logged in...
			redirectTo(UserServiceFactory.getUserService().createLoginURL("/user/profile"));
		}

	}
	
	abstract void buildUserPage(MainTemplate mainTemplate, UserProfileTemplate userProfileTemplate, RequestInformation requestInfo, UserLevel level, RequestType requestType);

	private void buildPage(MainTemplate mainTemplate, RequestInformation requestInfo, UserLevel level, RequestType requestType) {
		
		UserProfileTemplate userProfileTemplate = new UserProfileTemplate(requestInfo, level, requestInfo.getUser());
		buildUserPage(mainTemplate, userProfileTemplate, requestInfo, level, requestType);
		mainTemplate.setTemplate("body", userProfileTemplate);
	}

}
